package org.ch02.hw.demo09.converter;

import org.ch02.hw.demo09.TypeConverter;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 实现把查询数据转换为：java.time.LocalTime;
 *
 * @author ZJL
 * @date 2025-03-21
 */
public class LocalTimeConverter implements TypeConverter {
    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalTime.class);
    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        return switch (value) {
            case LocalTime localTime -> localTime;
            case LocalDateTime localDateTime -> localDateTime.toLocalTime();
            case Time time -> time.toLocalTime();
            case Timestamp timestamp -> timestamp.toLocalDateTime().toLocalTime();
            default -> null;
        };
    }
}
